﻿using CMS.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Models
{
    //物资表
    //继承接口IProvider(泛型的接口) 每个类对应每个表
    //物资实体传递过来<Cardgo>也就是CargoDBModel.edmx->CargoDBModel.tt->Cardgo.cs实体类
    public class CargoProvider : IProvider<Cardgo>//这里会报错然后我们实现接口把那个类创建方法快捷写过来
    {
        private CargorDBModel1 db = new CargorDBModel1();
        public int Delete(Cardgo t)
        {
            if (t == null) return 0;
            var model = db.Cardgo.ToList().FirstOrDefault(item => t.Id == item.Id);
            if (model == null) return 0;
            db.Cardgo.Remove(model);
            int count = db.SaveChanges();
            return count;
        }

        public int Insert(Cardgo t)
        {
            if (t == null) return 0;
            if (t.InserDate == null) return 0;
            db.Cardgo.Add(t);
            int count = db.SaveChanges();
            return count;
            //成功等于1 不成功0
        }

        public List<Cardgo> Select()
        {
            return db.Cardgo.ToList();
        }

        public int Update(Cardgo t)
        {
            if (t == null) return 0;
            var model = db.Cardgo.ToList().FirstOrDefault(item => t.Id == item.Id);
            if (model == null) return 0;
            model.TypeName= t.TypeName;
            model.Typeld= t.Typeld;
            model.UnitName= t.UnitName;
            model.UnitId= t.UnitId;
            model.Tag= t.Tag;
            model.Price= t.Price;
            int count = db.SaveChanges();
            return count;
        }
    }
}
